package com.resataurant.Controller.Admin;

import com.resataurant.Service.Impl.SaleDataServiceImpl;
import com.resataurant.Service.SaleDataService;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;

@WebServlet(name = "ReportFormServlet", value = "/ReportFormServlet")
public class ReportFormServlet extends HttpServlet {

    SaleDataService saleDataService = new SaleDataServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");


        String method=request.getParameter("method");
        switch (method){
            case "salesAll":
                Map<String, Integer> saleData = saleDataService.getSaleData();
                System.out.println(saleData.toString());
                request.getSession().setAttribute("saleDataAll", saleData);
                response.sendRedirect("/SaleDataAll.jsp");
                break;

            case "se":
                String start= request.getParameter("start");
                String end= request.getParameter("end");
                boolean flag= true;
                if (start.isEmpty() || end.isEmpty()){
                    flag=false;
                    request.getSession().setAttribute("message", "请输入日期");
                    response.sendRedirect("/Reportform.jsp");
                }
                if(start.compareTo(end)>0){
                    flag=false;
                    request.getSession().setAttribute("message", "开始日期不能大于结束日期");
                    response.sendRedirect("/Reportform.jsp");

                }
                if (flag){
                    request.getSession().setAttribute("start", start);
                    request.getSession().setAttribute("end", end);
                    Map<String, Integer> saleData1 = saleDataService.getSaleDataByDate(start,end);
                    request.getSession().setAttribute("saleDataBydate", saleData1);
                    response.sendRedirect("/SaleDataBydate.jsp");
                }
                break;

            case "popular":
                Map<String, Integer> list = saleDataService.getSalePopular();
                request.getSession().setAttribute("popularDish", list);
                List<Map.Entry<String,Integer>> list1 = new ArrayList<>(list.entrySet());
                Collections.sort(list1, new Comparator<Map.Entry<String, Integer>>() {
                    @Override
                    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                        return o2.getValue()-o1.getValue();
                    }
                });
                request.getSession().setAttribute("popularDishList", list1);
                response.sendRedirect("/PopularDish.jsp");
                break;

            case "userData":
                String id =request.getParameter("UserId");
                String username = request.getParameter("UserName");
                Map<String, Integer> userData = saleDataService.getSaleUser(id);
                System.out.println(userData.toString());
                request.getSession().setAttribute("userData", userData);
                request.getSession().setAttribute("SPusername", username);
                response.sendRedirect("/UserData.jsp");
                break;
        }

    }
}
